package com.example.newapplication.SealFragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.newapplication.Bean.Abnormal;
import com.example.newapplication.Fragment.HeadFragment;
import com.example.newapplication.R;

import java.util.ArrayList;
import java.util.List;

import static com.example.newapplication.Fragment.HeadFragment.mipmap;


public class FixFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<Abnormal> list;
    private Button button;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fix, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.recycler_fix);
        button = getActivity().findViewById(R.id.button_flag);

        list = new ArrayList<>();
        if(list!=null){
            list.clear();
        }
        Abnormal abnormal = new Abnormal();
        abnormal.equipmentName = "Apple iPhone Pro";
        abnormal.equipmentNum = "YD_2021_6853";
        abnormal.userName = "许一坚";
        abnormal.userNum = "2018010224";
        list.add(abnormal);
        abnormal = null;

        Abnormal abnormal2 = new Abnormal();
        abnormal2.equipmentName = "MI 10 Pro至尊版";
        abnormal2.equipmentNum = "YD_2021_686654";
        abnormal2.userName = "唐三皮";
        abnormal2.userNum = "2018010217";
        list.add(abnormal2);
        abnormal2=null;

//        Abnormal abnormal3 = new Abnormal();
//        abnormal3.equipmentName = "RAZER 雷蛇 标准";
//        abnormal3.equipmentNum = "CY_2021-5630";
//        abnormal3.userName = "周二吕";
//        abnormal3.userNum = "2018010243";
//        list.add(abnormal3);
//        abnormal3=null;

        recyclerView.addItemDecoration(new HeadFragment.SpacesItemDecoration(10));
        recyclerView.setAdapter(new mAdapter());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Abnormal abnormal4 = new Abnormal();
                abnormal4.equipmentName = "RAZER 雷蛇 标准";
                abnormal4.equipmentNum = "CY_2021-5630";
                abnormal4.userName = "周二吕";
                abnormal4.userNum = "2018010243";
                list.add(abnormal4);
                abnormal4=null;

                recyclerView.addItemDecoration(new HeadFragment.SpacesItemDecoration(10));
                recyclerView.setAdapter(new mAdapter());
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            }

        });
    }
    class mAdapter extends RecyclerView.Adapter<mViewHolder>{

        @NonNull
        @Override
        public mViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item3,parent,false);
            mViewHolder mviewHolder = new mViewHolder(view);
            return mviewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull mViewHolder holder, int position) {
            holder.img.setImageResource(mipmap[position]);
            holder.id.setText(list.get(position).equipmentNum);
            holder.name.setText(list.get(position).equipmentName);
            holder.id2.setText(list.get(position).userNum);
            holder.name2.setText(list.get(position).userName);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }
    class mViewHolder extends RecyclerView.ViewHolder {

        TextView id,name,id2,name2;
        ImageView img;
        public mViewHolder(@NonNull View itemView) {
            super(itemView);
            id = itemView.findViewById(R.id.textView36);
            img = itemView.findViewById(R.id.imageView4);
            name = itemView.findViewById(R.id.textView37);
            id2 = itemView.findViewById(R.id.textView34);
            name2 = itemView.findViewById(R.id.textView35);
        }
    }
}